#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std;

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> ret;
        sort(nums.begin(), nums.end());
        int len = nums.size();
        if (target > 0 && nums[len - 1] < 0) return ret;
        for (int i = 0; i < len;)
        {
            if (target < 0 && nums[i]>0) break;
            for (int j = i + 1; j < len;)
            {
                long long tmp = (long long)target - nums[i] - nums[j];
                int left = j + 1;
                int right = len - 1;
                while (left < right)
                {
                    if (nums[left] + nums[right] == tmp)
                    {
                        ret.push_back({ nums[i],nums[j],nums[left],nums[right] });
                        left++;
                        right--;
                        while (left < right && nums[left] == nums[left - 1]) left++;
                        while (left < right && nums[right] == nums[right + 1]) right--;
                    }
                    else if (nums[left] + nums[right] < tmp) left++;
                    else right--;
                }
                j++;
                while (j < len && nums[j] == nums[j - 1]) j++;
            }
            i++;
            while (i < len && nums[i] == nums[i - 1]) i++;
        }
        return ret;
    }
};


int main()
{
	return 0;
}
